home *** CD-ROM | disk | FTP | other *** search
/ Internet Surfer 2.0 / Internet Surfer 2.0 (Wayzata Technology) (1996).iso / pc / text / mac / faqs.432 < prev    next >
Encoding:
Text File  |  1996-02-12  |  27.8 KB  |  744 lines

  1. Frequently Asked Questions (FAQS);faqs.432
  2.  
  3.  
  4.  
  5.  
  6. 1.17) Is there a Perl profiler?
  7.  
  8.     While there isn't one included with the perl source distribution,
  9.     various folks have written packages that allow you to do at least some
  10.     sort of profiling.  The strategy usually includes modifying the perl
  11.     debugger to handle profiling.  Authors of these packages include
  12.  
  13.     Wayne Thompson         <me@anywhere.EBay.Sun.COM>
  14.     Ray Lischner         <lisch@sysserver1.mentor.com>
  15.     Kresten Krab Thorup     <krab@iesd.auc.dk>
  16.  
  17.     The original articles by these folks containing their
  18.     profilers are available on convex.com in
  19.     /pub/perl/information/profiling.shar via anon ftp.
  20.  
  21.  
  22. 1.18) Is there a yacc for Perl?
  23.  
  24.     Yes!! It's a version of Berkeley yacc that outputs Perl code instead
  25.     of C code!  You can get this from ftp.sterling.com [192.124.9.1] in
  26.     /local/perl-byacc1.8.1.tar.Z, or send the author mail for details.
  27.  
  28.  
  29. 1.19) How can I use curses with perl?
  30.  
  31.     One way is to build a curseperl binary by linking in your C curses
  32.     library as described in the usub subdirectory of the perl sources.
  33.     This requires a modicum of work, but it will be reasonably fast
  34.     since it's all in C (assuming you consider curses reasonably fast. :-)
  35.     Programs written using this method require the modified curseperl,
  36.     not vanilla perl, to run.  While this is something of a disadvantage,
  37.     experience indicates that it's better to use curseperl than to
  38.     try to roll your own using termcap directly.
  39.  
  40.     Another possibility is to use Henk Penning's cterm package, a curses
  41.     emulation library written in perl.  cterm is actually a separate
  42.     program with which you communicate via a pipe.  It is available from
  43.     ftp.cs.ruu.nl [131.211.80.17] via anonymous ftp. in the directory
  44.     pub/PERL.  You may also acquire the package via email in compressed,
  45.     uuencoded form by sending a message to mail-server@cs.ruu.nl
  46.     containing these lines:
  47.  
  48.     begin
  49.     send PERL/cterm.shar.Z
  50.     end
  51.  
  52.     See the question on retrieving perl via mail for more information on
  53.     how to get retrieve other items of interest from the mail server
  54.     there.
  55.  
  56.  
  57. 1.20) How can I use X with Perl?
  58.  
  59.     Right now, you have several choices.  You can wait for perl5, use
  60.     the WAFE or STDWIN packages, or try to make your own usub bindings.
  61.  
  62.     Perl5 is anticipated to be released with bindings for X, called
  63.     guiperl.  An exciting prototype for this, written by Jon Biggar
  64.     <jon@netlabs.com>, Larry's *other* brother-in-law and officemate,
  65.     is already up and running inside of Netlabs.  This program addresses
  66.     the same dynamic gui-building problem space as does tcl/tk.
  67.  
  68.     If you can't wait or don't think that guiperl will do what you want,
  69.     a stab at Motif bindings was begun by Theodore C. Law
  70.     <TEDLAW@TOROLAB6.VNET.IBM.COM> area.  His article about this is
  71.     on convex.com in /pub/perl/info/motif for anon ftp.
  72.  
  73.     STDWIN is a library written by Guido van Rossum <guido@cwi.nl>
  74.     (author of the Python programming language) that is portable
  75.     between Mac, Dos and X11.  One could write a Perl agent to
  76.     speak to this STDWIN server.
  77.  
  78.     WAFE is a package that implements a symbolic interface to the Athena
  79.     widgets (X11R5). A typical Wafe application consists in our framework
  80.     of two parts: the front-end (we call it Wafe for Widget[Athena]front
  81.     end) and an application program running typically as separate process.
  82.     The application program can be implemented in an arbitrary programming
  83.     language and talks to the front-end via stdio.  Since Wafe (the
  84.     front-end) was developed using the extensible TCL shell (cite John
  85.     Ousterhout), an application program can dynamically submit requests to
  86.     the front-end to build up the graphical user interface; the
  87.     application can even down-load application specific procedures into
  88.     the front-end.  The distribution contains sample application programs
  89.     in Perl, GAWK, Prolog, TCL, and C talking to the same Wafe binary.
  90.     Many of the demo applications are implemented in Perl.  Wafe 0.9 can
  91.     be obtained via anonymous ftp from
  92.     ftp.wu-wien.ac.at:pub/src/X11/wafe-0.9.tar.Z
  93.     (for people without name server: the ip address is 137.208.3.5)
  94.  
  95.  
  96. 1.21) What is perl4?  What is perl5?
  97.  
  98.     The answer to what is perl4 is nearly anything you might otherwise
  99.     program in shell or C.  The answer to what is perl5 is basically
  100.     Perl: the Next Generation.  In fact, it's essentially a complete
  101.     rewrite of perl from the bottom up, and back again.
  102.  
  103.     Larry gave a talk on perl5 at a Bay LISA meeting as well as at the
  104.     most recent USENIX LISA conference in Long Beach in which he timorously
  105.     admitted that perl5 might possibly be beta released in early 1993.
  106.     He enumerated some of the following features.  Note that not only have
  107.     not all these been implemented yet, the ones further down the list
  108.     might well not get done at all.
  109.  
  110.     a faster, tighter, more flexible interpreter
  111.     very easy GUI Perl applications using X bindings ("guiperl")
  112.     embeddable Perl code in C code: cc prog.c -lperl
  113.     multiple coresident perl interpreters:
  114.         perhaps threading and/or coroutines
  115.     named argument passing:
  116.         some_func( OC => $red, TOF => "\f");
  117.     recursive lists:
  118.         [a, b, [c, d], e] has 4 elts, the 3rd being itself a list
  119.     typed pointers and generalized indirection:
  120.         like @{$aptr} or &{$fptr} or &{ $table[$index] . "func" }().
  121.     merging of list operator and function calling syntax:
  122.         split /pat/, $string;
  123.     subroutines without &'s:   myfunc($arg);
  124.     generalization of dbm binding for assoc arrays to handle
  125.         any generic fetch/store/open/close/flush package.
  126.         (thus allowing both dbm and gdbm at once)
  127.     object oriented programming:
  128.         STDOUT->flush(1);
  129.         give dog $bone;
  130.     lexical scoping
  131.     dynamic loading of C libraries for systems that can
  132.     byte-compiled code for speed and maybe security
  133.  
  134.     It's tempting to want this stuff soon, since the sooner it comes
  135.     out the sooner we can all build really cool applications.  But the
  136.     longer Larry works on it, the more items from this list will actually
  137.     get done, and the more robust the release will be.  So let's not
  138.     ask him about it too often.
  139.  
  140.  
  141. 1.22) How does Perl compare with languages like REXX or TCL?
  142.  
  143.     REXX is an interpreted programming language first seen on IBM systems,
  144.     and TCL is John Ousterhout's embeddable command language.  TCL's most
  145.     intriguing feature for many people is the tcl/tk toolset that allows
  146.     for interpreted X-based tools.
  147.  
  148.     To avoid any flamage, if you really want to know the answer to this
  149.     question, probably the best thing to do is try to write equivalent
  150.     code to do a set of tasks.  All three have their own newsgroups in
  151.     which you can learn about (but hopefully not argue about) these
  152.     languages.
  153.  
  154.     To find out more about these or other languages, you might also check
  155.     out David Muir Sharnoff <muir@tfs.com>'s posting on "Catalog of
  156.     compilers, interpreters, and other language tools" which he posts to
  157.     comp.lang.misc, comp.sources.d, comp.archives.admin, and the
  158.     news.answers newsgroups.  It's a comprehensive treatment of many
  159.     different languages.  (Caveat lector: he considers Perl's syntax
  160.     "unappealing".)   This list is archived on convex.com in
  161.     /pub/perl/info/lang-survey.shar .
  162.  
  163.  
  164. 1.23) Is it a Perl program or a Perl script?
  165.  
  166.     Certainly. :-)
  167.  
  168.     Current UNIX parlance holds that anything interpreted
  169.     is a script, and anything compiled into native machine
  170.     code is a program.  However, others hold that a program
  171.     is a program is a program: after all, one seldom discusses
  172.     scripts written in BASIC or LISP.  Larry considers it
  173.     a program if it's set in stone and you can't change it,
  174.     whereas if you go in and hack on it, then it's a script.
  175.  
  176.     But doesn't really matter.  The terms are generally
  177.     interchangeable today.
  178.  
  179.  
  180. 1.24) What's the difference between "Perl" and "perl"?
  181.  
  182.      32 :-)  [  ord('p') - ord('P')  ]
  183.  
  184.      Larry now uses "Perl" to signify the language proper and "perl" the
  185.      implementation of it, i.e. the current interpreter.  Hence my quip
  186.      that "Nothing but perl can parse Perl."
  187.  
  188.      On the other hand, the aesthetic value of casewise parallelism
  189.      in "awk", "sed", and "perl" as much require the lower-case
  190.      version as "C", "Pascal", and "Perl" require the
  191.      upper-case version.  It's also easier to type "Perl" in
  192.      typeset print than to be constantly switching in Courier. :-)
  193.  
  194.      In other words, it doesn't matter much, especially if all
  195.      you're doing is hearing someone talk about the language;
  196.      case is hard to distingish aurally.
  197.  
  198.  
  199. 1.25) What companies use or ship Perl?
  200.  
  201.     At this time, the known list of companies that ship Perl includes at
  202.     least the following:
  203.  
  204.     BSDI
  205.     Comdisco Systems
  206.     CONVEX Computer Corporation
  207.     Dell
  208.     Integraph
  209.     Kubota Pacific (/usr/contrib)
  210.     Netlabs
  211.  
  212.     Many other companies use Perl internally for purposes of tools
  213.     development, systems administration, installation scripts, and test
  214.     suites.  Rumor has it that the large workstation vendors (the TLA set)
  215.     are seriously looking into shipping Perl with their standard systems
  216.     "soon".
  217.  
  218.     People with support contracts with their vendors are actively
  219.     encouraged to submit enhancement requests that Perl be shipped
  220.     as part of their standard system.  It would, at the very least,
  221.     reduce the FTP load on the Internet. :-)
  222.  
  223.  
  224. 1.26) Is there commercial, 3rd-party support for Perl?
  225.  
  226.     No.  Although perl is included in the GNU distribution, at last check,
  227.     Cygnus does not offer support for it.  However, it's unclear whether
  228.     they've ever been offered sufficient financial incentive to do so.
  229.  
  230.     On the other hand, you do have comp.lang.perl as a totally gratis
  231.     support mechanism.  As long as you ask "interesting" questions,
  232.     you'll probably get plenty of help. :-)
  233.  
  234. 1.27) Where can I get a list of the JAPH signature quotes?
  235.  
  236.     These are the "just another perl hacker" signatures that
  237.     some people sign their postings with.  About 100 of the
  238.     of the earlier ones are on convex.com in /pub/perl/info/japh.
  239.  
  240. 1.28) Where can I get a list of Larry Wall witticisms?
  241.  
  242.     Over a hundred quips by Larry, from postings of his or source code,
  243.     can be found on convex.com in /pub/perl/info/lwall-quotes.
  244.  
  245. 1.29) Is there a pretty-printer for Perl?
  246.  
  247.     That depends on what you mean.  If you want something
  248.     that works like vgrind on Perl programs, then the answer
  249.     is "yes, nearly".  Here's a vgrind entry for perl:
  250.  
  251.     PERL|perl|Perl:\
  252.         :pb=(^\d?sub\d\p\d?|\{):\
  253.         :bb={:be=}:cb=#:ce=$:sb=":se=\e":lb=':\
  254.         :le=\e':tl:\
  255.         :id=_:\
  256.         :kw=\
  257.         if for foreach unless until while continue else elsif \
  258.         do eval require \
  259.         die exit \
  260.         defined delete reset \
  261.         goto last redo next dump \
  262.         local undef return  \
  263.         write format  \
  264.         sub package
  265.  
  266.     It doesn't actually do everything right; in particular,
  267.     things like $#, $', s#/foo##, and $foo'bar all confuse it.
  268.  
  269.     If what you mean is whether there is a program that will
  270.     reformat the program much as indent(1) will do for C, then
  271.     the answer is no.  The complex feedback between the scanner
  272.     and the parser (as in the things that confuse vgrind) make
  273.     it challenging to write at best to write a stand-alone C parser.
  274. --
  275.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  276.  
  277.     California is a fine place to live -- if you happen to be an orange.
  278.             -- Fred Allen
  279. --
  280.     Tom Christiansen      tchrist@convex.com      convex!tchrist
  281.  
  282.     As far as we know, our computer has never had an undetected error.
  283.             -- Weisert
  284. Xref: bloom-picayune.mit.edu comp.lang.perl:14079 news.answers:4268
  285. Path: bloom-picayune.mit.edu!enterpoop.mit.edu!usc!cs.utexas.edu!uunet!olivea!pagesat!spssig.spss.com!news.oc.com!convex!tchrist
  286. From: tchrist@convex.COM (Tom Christiansen)
  287. Newsgroups: comp.lang.perl,news.answers
  288. Subject: comp.lang.perl FAQ (part 2 of 2)
  289. Message-ID: <1992Nov30.130440.11167@news.eng.convex.com>
  290. Date: 30 Nov 92 13:04:40 GMT
  291. Expires: Mon, 4 Jan 1993 12:00:00 GMT
  292. References: <1992Nov30.124619.8579@news.eng.convex.com>
  293. Sender: usenet@news.eng.convex.com (news access account)
  294. Reply-To: tchrist@convex.COM (Tom Christiansen)
  295. Followup-To: comp.lang.perl
  296. Organization: Convex Computer Corporation, Colorado Springs, CO
  297. Lines: 1348
  298. Approved: news-answers-request@MIT.Edu
  299. Originator: tchrist@pixel.convex.com
  300. Nntp-Posting-Host: pixel.convex.com
  301. X-Disclaimer: This message was written by a user at CONVEX Computer
  302.               Corp. The opinions expressed are those of the user and
  303.               not necessarily those of CONVEX.
  304.  
  305. Archive-name: perl-faq/part2
  306. Version: $Id: perl-tech,v 1.2 92/11/30 05:22:44 tchrist Exp Locker: tchrist $
  307.  
  308. This posting contains answers to the following techical questions
  309. regarding Perl:
  310.  
  311. 2.1) What are all these $@*%<> signs and how do I know when to use them?
  312. 2.2) Why don't backticks work as they do in shells?
  313. 2.3) How come Perl operators have different precedence than C operators?
  314. 2.4) How come my converted awk/sed/sh script runs more slowly in Perl?
  315. 2.5) How can I call my system's unique C functions from Perl?
  316. 2.6) Where do I get the include files to do ioctl() or syscall()?
  317. 2.7) Why doesn't "local($foo) = <FILE>;" work right?
  318. 2.8) How can I detect keyboard input without reading it?
  319. 2.9) How can I make an array of arrays or other recursive data types?
  320. 2.10) How can I quote a variable to use in a regexp?
  321. 2.11) Why do setuid Perl scripts complain about kernel problems?
  322. 2.12) How do I open a pipe both to and from a command?
  323. 2.13) How can I change the first N letters of a string?
  324. 2.14) How can I manipulate fixed-record-length files?
  325. 2.15) How can I make a file handle local to a subroutine?
  326. 2.16) How can I extract just the unique elements of an array?
  327. 2.17) How can I call alarm() or usleep() from Perl?
  328. 2.18) How can I test whether an array contains a certain element?
  329. 2.19) How can I do an atexit() or setjmp()/longjmp() in Perl?
  330. 2.20) Why doesn't Perl interpret my octal data octally?
  331. 2.21) How do I sort an associative array by value instead of by key?
  332. 2.22) How can I capture STDERR from an external command?
  333. 2.23) Why doesn't open return an error when a pipe open fails?
  334. 2.24) How can I compare two date strings?
  335. 2.25) What's the fastest way to code up a given task in perl?
  336. 2.26) How can I know how many entries are in an associative array?
  337. 2.27) Why can't my perl program read from STDIN after I gave it ^D (EOF) ?
  338. 2.28) Do I always/never have to quote my strings or use semicolons?
  339. 2.29) How can I translate tildes in a filename?
  340. 2.30) How can I convert my shell script to Perl?
  341. 2.31) What is variable suicide and how can I prevent it?
  342. 2.32) Can I use Perl regular expressions to match balanced text?
  343. 2.33) Can I use Perl to run a telnet or ftp session?
  344. 2.34) What does "Malformed command links" mean?
  345. 2.35) How can I set up a footer format to be used with write()?
  346. 2.36) Why does my Perl program keep growing in size?
  347.  
  348.  
  349. 2.1) What are all these $@*%<> signs and how do I know when to use them?
  350.  
  351.     Those are type specifiers: $ for scalar values, @ for indexed arrays,
  352.     and % for hashed arrays.  The * means all types of that symbol name
  353.     and are sometimes used like pointers; the <> are used for inputting
  354.     a record from a filehandle.  See the question on arrays of arrays
  355.     for more about Perl pointers.
  356.  
  357.     Always make sure to use a $ for single values and @ for multiple ones.
  358.     Thus element 2 of the @foo array is accessed as $foo[2], not @foo[2],
  359.     which is a list of length one (not a scalar), and is a fairly common
  360.     novice mistake.  Sometimes you can get by with @foo[2], but it's
  361.     not really doing what you think it's doing for the reason you think
  362.     it's doing it, which means one of these days, you'll shoot yourself
  363.     in the foot; ponder for a moment what these will really do:
  364.     @foo[0] = `cmd args`;
  365.     @foo[2] = <FILE>;
  366.     Just always say $foo[2] and you'll be happier.
  367.  
  368.     This may seem confusing, but try to think of it this way:  you use the
  369.     character of the type which you *want back*.  You could use @foo[1..3] for
  370.     a slice of three elements of @foo, or even @foo{A,B,C} for a slice of
  371.     of %foo.  This is the same as using ($foo[1], $foo[2], $foo[3]) and
  372.     ($foo{A}, $foo{B}, $foo{C}) respectively.  In fact, you can even use
  373.     lists to subscript arrays and pull out more lists, like @foo[@bar] or
  374.     @foo{@bar}, where @bar is in both cases presumably a list of subscripts.
  375.  
  376.     While there are a few places where you don't actually need these type
  377.     specifiers, except for files, you should always use them.  Note that
  378.     <FILE> is NOT the type specifier for files; it's the equivalent of awk's
  379.     getline function, that is, it reads a line from the handle FILE.  When
  380.     doing open, close, and other operations besides the getline function on
  381.     files, do NOT use the brackets.
  382.  
  383.     Beware of saying:
  384.     $foo = BAR;
  385.     Which wil be interpreted as
  386.     $foo = 'BAR';
  387.     and not as
  388.     $foo = <BAR>;
  389.     If you always quote your strings, you'll avoid this trap.
  390.  
  391.     Normally, files are manipulated something like this (with appropriate
  392.     error checking added if it were production code):
  393.  
  394.     open (FILE, ">/tmp/foo.$$");
  395.     print FILE "string\n";
  396.     close FILE;
  397.  
  398.     If instead of a filehandle, you use a normal scalar variable with file
  399.     manipulation functions, this is considered an indirect reference to a
  400.     filehandle.  For example,
  401.  
  402.     $foo = "TEST01";
  403.     open($foo, "file");
  404.  
  405.     After the open, these two while loops are equivalent:
  406.  
  407.     while (<$foo>) {}
  408.     while (<TEST01>) {}
  409.  
  410.     as are these two statements:
  411.     
  412.     close $foo;
  413.     close TEST01;
  414.  
  415.     but NOT to this:
  416.  
  417.     while (<$TEST01>) {} # error
  418.         ^
  419.         ^ note spurious dollar sign
  420.  
  421.     This is another common novice mistake; often it's assumed that
  422.  
  423.     open($foo, "output.$$");
  424.  
  425.     will fill in the value of $foo, which was previously undefined.
  426.     This just isn't so -- you must set $foo to be the name of a valid
  427.     filehandle before you attempt to open it.
  428.  
  429.  
  430. 2.2) Why don't backticks work as they do in shells?
  431.  
  432.     Several reason.  One is because backticks do not interpolate within
  433.     double quotes in Perl as they do in shells.
  434.  
  435.     Let's look at two common mistakes:
  436.  
  437.          $foo = "$bar is `wc $file`";  # WRONG
  438.  
  439.     This should have been:
  440.  
  441.      $foo = "$bar is " . `wc $file`;
  442.  
  443.     But you'll have an extra newline you might not expect.  This
  444.     does not work as expected:
  445.  
  446.       $back = `pwd`; chdir($somewhere); chdir($back); # WRONG
  447.  
  448.     Because backticks do not automatically eat trailing or embedded
  449.     newlines.  The chop() function will remove the last character from
  450.     a string.  This should have been:
  451.  
  452.       chop($back = `pwd`); chdir($somewhere); chdir($back);
  453.  
  454.     You should also be aware that while in the shells, embedding
  455.     single quotes will protect variables, in Perl, you'll need
  456.     to escape the dollar signs.
  457.  
  458.     Shell: foo=`cmd 'safe $dollar'`
  459.     Perl:  $foo=`cmd 'safe \$dollar'`;
  460.     
  461.  
  462. 2.3) How come Perl operators have different precedence than C operators?
  463.  
  464.     Actually, they don't; all C operators have the same precedence in Perl as
  465.     they do in C.  The problem is with a class of functions called list
  466.     operators, e.g. print, chdir, exec, system, and so on.  These are somewhat
  467.     bizarre in that they have different precedence depending on whether you
  468.     look on the left or right of them.  Basically, they gobble up all things
  469.     on their right.  For example,
  470.  
  471.     unlink $foo, "bar", @names, "others";
  472.  
  473.     will unlink all those file names.  A common mistake is to write:
  474.  
  475.     unlink "a_file" || die "snafu";
  476.  
  477.     The problem is that this gets interpreted as
  478.  
  479.     unlink("a_file" || die "snafu");
  480.  
  481.     To avoid this problem, you can always make them look like function calls
  482.     or use an extra level of parentheses:
  483.  
  484.     (unlink "a_file") || die "snafu";
  485.     unlink("a_file")  || die "snafu";
  486.  
  487.     Sometimes you actually do care about the return value:
  488.  
  489.     unless ($io_ok = print("some", "list")) { }
  490.  
  491.     Yes, print() return I/O success.  That means
  492.  
  493.     $io_ok = print(2+4) * 5;
  494.  
  495.     returns 5 times whether printing (2+4) succeeded, and
  496.     print(2+4) * 5;
  497.     returns the same 5*io_success value and tosses it.
  498.  
  499.     See the Perl man page's section on Precedence for more gory details,
  500.     and be sure to use the -w flag to catch things like this.
  501.  
  502.  
  503. 2.4) How come my converted awk/sed/sh script runs more slowly in Perl?
  504.  
  505.     The natural way to program in those languages may not make for the fastest
  506.     Perl code.  Notably, the awk-to-perl translator produces sub-optimal code;
  507.     see the a2p man page for tweaks you can make.
  508.  
  509.     Two of Perl's strongest points are its associative arrays and its regular
  510.     expressions.  They can dramatically speed up your code when applied
  511.     properly.  Recasting your code to use them can help a lot.
  512.  
  513.     How complex are your regexps?  Deeply nested sub-expressions with {n,m} or
  514.     * operators can take a very long time to compute.  Don't use ()'s unless
  515.     you really need them.  Anchor your string to the front if you can.
  516.  
  517.     Something like this:
  518.     next unless /^.*%.*$/;
  519.     runs more slowly than the equivalent:
  520.     next unless /%/;
  521.  
  522.     Note that this:
  523.     next if /Mon/;
  524.     next if /Tue/;
  525.     next if /Wed/;
  526.     next if /Thu/;
  527.     next if /Fri/;
  528.     runs faster than this:
  529.     next if /Mon/ || /Tue/ || /Wed/ || /Thu/ || /Fri/;
  530.     which in turn runs faster than this:
  531.     next if /Mon|Tue|Wed|Thu|Fri/;
  532.     which runs *much* faster than:
  533.     next if /(Mon|Tue|Wed|Thu|Fri)/;
  534.  
  535.     There's no need to use /^.*foo.*$/ when /foo/ will do.
  536.  
  537.     Remember that a printf costs more than a simple print.
  538.  
  539.     Don't split() every line if you don't have to.
  540.  
  541.     Another thing to look at is your loops.  Are you iterating through
  542.     indexed arrays rather than just putting everything into a hashed
  543.     array?  For example,
  544.  
  545.     @list = ('abc', 'def', 'ghi', 'jkl', 'mno', 'pqr', 'stv');
  546.  
  547.     for $i ($[ .. $#list) {
  548.         if ($pattern eq $list[$i]) { $found++; }
  549.     }
  550.  
  551.     First of all, it would be faster to use Perl's foreach mechanism
  552.     instead of using subscripts:
  553.  
  554.     foreach $elt (@list) {
  555.         if ($pattern eq $elt) { $found++; }
  556.     }
  557.  
  558.     Better yet, this could be sped up dramatically by placing the whole
  559.     thing in an associative array like this:
  560.  
  561.     %list = ('abc', 1, 'def', 1, 'ghi', 1, 'jkl', 1,
  562.          'mno', 1, 'pqr', 1, 'stv', 1 );
  563.     $found += $list{$pattern};
  564.  
  565.     (but put the %list assignment outside of your input loop.)
  566.  
  567.     You should also look at variables in regular expressions, which is
  568.     expensive.  If the variable to be interpolated doesn't change over the
  569.     life of the process, use the /o modifier to tell Perl to compile the
  570.     regexp only once, like this:
  571.  
  572.     for $i (1..100) {
  573.         if (/$foo/o) {
  574.         &some_func($i);
  575.         }
  576.     }
  577.  
  578.     Finally, if you have a bunch of patterns in a list that you'd like to
  579.     compare against, instead of doing this:
  580.  
  581.     @pats = ('_get.*', 'bogus', '_read', '.*exit', '_write');
  582.     foreach $pat (@pats) {
  583.         if ( $name =~ /^$pat$/ ) {
  584.         &some_func();
  585.         last;
  586.         }
  587.     }
  588.  
  589.     If you build your code and then eval it, it will be much faster.
  590.     For example:
  591.  
  592.     @pats = ('_get.*', 'bogus', '_read', '.*exit', '_write');
  593.     $code = <<EOS
  594.         while (<>) {
  595.             study;
  596. EOS
  597.     foreach $pat (@pats) {
  598.         $code .= <<EOS
  599.         if ( /^$pat\$/ ) {
  600.             &some_func();
  601.             next;
  602.         }
  603. EOS
  604.     }
  605.     $code .= "}\n";
  606.     print $code if $debugging;
  607.     eval $code;
  608.  
  609.  
  610.  
  611. 2.5) How can I call my system's unique C functions from Perl?
  612.  
  613.     If these are system calls and you have the syscall() function, then
  614.     you're probably in luck -- see the next question.  For arbitrary
  615.     library functions, it's not quite so straight-forward.  While you
  616.     can't have a C main and link in Perl routines, if you're
  617.     determined, you can extend Perl by linking in your own C routines.
  618.     See the usub/ subdirectory in the Perl distribution kit for an example
  619.     of doing this to build a Perl that understands curses functions.  It's
  620.     neither particularly easy nor overly-documented, but it is feasible.
  621.  
  622.  
  623. 2.6) Where do I get the include files to do ioctl() or syscall()?
  624.  
  625.     These are generated from your system's C include files using the h2ph
  626.     script (once called makelib) from the Perl source directory.  This will
  627.     make files containing subroutine definitions, like &SYS_getitimer, which
  628.     you can use as arguments to your function.
  629.  
  630.     You might also look at the h2pl subdirectory in the Perl source for how to
  631.     convert these to forms like $SYS_getitimer; there are both advantages and
  632.     disadvantages to this.  Read the notes in that directory for details.
  633.  
  634.     In both cases, you may well have to fiddle with it to make these work; it
  635.     depends how funny-looking your system's C include files happen to be.
  636.  
  637.     If you're trying to get at C structures, then you should take a look
  638.     at using c2ph, which uses debugger "stab" entries generated by your
  639.     BSD or GNU C compiler to produce machine-independent perl definitions
  640.     for the data structures.  This allows to you avoid hardcoding
  641.     structure layouts, types, padding, or sizes, greatly enhancing
  642.     portability.  c2ph comes with the perl distribution.  On an SCO
  643.     system, GCC only has COFF debugging support by default, so you'll have
  644.     to build GCC 2.1 with DBX_DEBUGGING_INFO defined, and use -gstabs to
  645.     get c2ph to work there.
  646.  
  647.     See the file /pub/perl/info/ch2ph on convex.com via anon ftp
  648.     for more traps and tips on this process.
  649.  
  650.  
  651. 2.7) Why doesn't "local($foo) = <FILE>;" work right?
  652.  
  653.     Well, it does.  The thing to remember is that local() provides an array
  654.     context, and that the <FILE> syntax in an array context will read all the
  655.     lines in a file.  To work around this, use:
  656.  
  657.     local($foo);
  658.     $foo = <FILE>;
  659.  
  660.     You can use the scalar() operator to cast the expression into a scalar
  661.     context:
  662.  
  663.     local($foo) = scalar(<FILE>);
  664.  
  665.  
  666. 2.8) How can I detect keyboard input without reading it?
  667.  
  668.     You should check out the Frequently Asked Questions list in
  669.     comp.unix.* for things like this: the answer is essentially the same.
  670.     It's very system dependent.  Here's one solution that works on BSD
  671.     systems:
  672.  
  673.     sub key_ready {
  674.         local($rin, $nfd);
  675.         vec($rin, fileno(STDIN), 1) = 1;
  676.         return $nfd = select($rin,undef,undef,0);
  677.     }
  678.  
  679.     A closely related question is how to input a single character from the
  680.     keyboard.  Again, this is a system dependent operation.  The following
  681.     code that may or may not help you:
  682.  
  683.     $BSD = -f '/vmunix';
  684.     if ($BSD) {
  685.         system "stty cbreak </dev/tty >/dev/tty 2>&1";
  686.     }
  687.     else {
  688.         system "stty", '-icanon',
  689.         system "stty", 'eol', "\001";
  690.     }
  691.  
  692.     $key = getc(STDIN);
  693.  
  694.     if ($BSD) {
  695.         system "stty -cbreak </dev/tty >/dev/tty 2>&1";
  696.     }
  697.     else {
  698.         system "stty", 'icanon';
  699.         system "stty", 'eol', '^@'; # ascii null
  700.     }
  701.     print "\n";
  702.  
  703.     You could also handle the stty operations yourself for speed if you're
  704.     going to be doing a lot of them.  This code works to toggle cbreak
  705.     and echo modes on a BSD system:
  706.  
  707.     sub set_cbreak { # &set_cbreak(1) or &set_cbreak(0)
  708.     local($on) = $_[0];
  709.     local($sgttyb,@ary);
  710.     require 'sys/ioctl.ph';
  711.     $sgttyb_t   = 'C4 S' unless $sgttyb_t;  # c2ph: &sgttyb'typedef()
  712.  
  713.     ioctl(STDIN,&TIOCGETP,$sgttyb) || die "Can't ioctl TIOCGETP: $!";
  714.  
  715.     @ary = unpack($sgttyb_t,$sgttyb);
  716.     if ($on) {
  717.         $ary[4] |= &CBREAK;
  718.         $ary[4] &= ~&ECHO;
  719.     } else {
  720.         $ary[4] &= ~&CBREAK;
  721.         $ary[4] |= &ECHO;
  722.     }
  723.     $sgttyb = pack($sgttyb_t,@ary);
  724.  
  725.     ioctl(STDIN,&TIOCSETP,$sgttyb) || die "Can't ioctl TIOCSETP: $!";
  726.     }
  727.  
  728.     Note that this is one of the few times you actually want to use the
  729.     getc() function; it's in general way too expensive to call for normal
  730.     I/O.  Normally, you just use the <FILE> syntax, or perhaps the read()
  731.     or sysread() functions.
  732.  
  733.     For perspectives on more portable solutions, use anon ftp to retrieve
  734.     the file /pub/perl/info/keypress from convex.com.
  735.  
  736.  
  737. 2.9) How can I make an array of arrays or other recursive data types?
  738.  
  739.     Remember that Perl isn't about nested data structures (actually,
  740.     perl0 ..  perl4 weren't, but maybe perl5 will be, at least
  741.     somewhat).  It's about flat ones, so if you're trying to do this, you
  742.     may be going about it the wrong way or using the wrong tools.  You
  743.     might try parallel arrays with common subscripts.
  744.